// pages/harvestAddress/harvestAddress.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navtop:"",
    openid:1,
    num:[],
  },

  onLoad(){
    const app =getApp();
       // 导航栏
    // 1.获取屏幕可使用宽度
    let windowWidth = wx.getSystemInfoSync().windowWidth;
    // 2.获取状态栏高度
    const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
    //3.获取胶囊按钮位置信息
    const menuButton = wx.getMenuButtonBoundingClientRect();
    this.setData({
      navtop:menuButton.top,
       openid: app.globalData.openid,
    })
   
    console.log(this.data.openid);

  },
  onShow(){
    this.getdata1();
  },

  gitfor(){
    wx.navigateBack({
      delta: -1
    })
  },
new: function() {
  wx.navigateTo({
    url: '../addAddress/addAddress'
  })
}
,
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  getdata1(){
    const that = this;
    wx.request({
      url: 'https://dj.ifankui.com/api/usrEn/getDeliveryAddress',
      method:'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data:{
        openid:this.data.openid,
      },
      success(res){
          console.log(res);
          that.setData({
            num:res.data.data
          })
          console.log(that.data.num);
      }
    })
  },
  getinfo(e){
    const item = e.currentTarget.dataset;
    console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: '../modifyaddress/modifyaddress?id=' + item.id+'&addr='+item.addr+'&area='+item.area+'&city='+item.city+'&xm='+item.name+'&sjh='+item.sjh+'&telphone='+item.telphone+'&province='+item.province
    })
  },
  dele(e){
    const that =this
    const itemid = e.currentTarget.dataset.id;
    wx.request({
      url: 'https://dj.ifankui.com/api/usrEn/deleteDeliveryAddress',
      method:'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data:{
        openid:this.data.openid,
        addresId:itemid
      },
      success(res){
        console.log(res);
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        });
        that.getdata1()
      }
    })
  },

  yijian() {
    const that =this
    const app = getApp();
    console.log('123');
  
    // 检查用户是否已经授权
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.address']) {
          // 如果用户没有授权，引导用户进行授权
          wx.authorize({
            scope: 'scope.address',
            success() {
              // 用户同意授权后，调用 chooseAddress
              wx.chooseAddress({
                success(res) {
                  that.handleChooseAddressSuccess(res, app);
                },
                fail(err) {
                  console.log('chooseAddress 调用失败:', err);
                }
              });
            },
            fail() {
              // 用户拒绝授权，引导用户到设置页面进行授权
              wx.showModal({
                title: '提示',
                content: '请授权使用地址功能',
                success(modalRes) {
                  if (modalRes.confirm) {
                    wx.openSetting({
                      success(settingRes) {
                        if (settingRes.authSetting['scope.address']) {
                          // 用户在设置页面同意授权后，调用 chooseAddress
                          wx.chooseAddress({
                            success(res) {
                              that.handleChooseAddressSuccess(res, app);
                            },
                            fail(err) {
                              console.log('chooseAddress 调用失败:', err);
                              console.log(111);
                            }
                          });
                        }
                      }
                    });
                  }
                }
              });
            }
          });
        } else {
          // 用户已经授权，直接调用 chooseAddress
          wx.chooseAddress({
            success(res) {
              that.handleChooseAddressSuccess(res, app);
            },
            fail(err) {
              console.log('chooseAddress 调用失败:', err);
            }
          });
        }
      },
      fail(err) {
        console.log('getSetting 调用失败:', err);
      }
    });
  },
  
  handleChooseAddressSuccess(res, app) {
    console.log(res);
    console.log(1231231);
    wx.request({
      url: 'https://dj.ifankui.com/api/usrEn/addDeliveryAddress',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        openid: app.globalData.openid,
        xm: res.userName,
        sjh: res.telNumber,
        telphone: 11111111,
        province: res.provinceName,
        city: res.cityName,
        area: res.countyName,
        addr: res.detailInfo,
      },
      success(res) {
        console.log(res);
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        });
      }
    });
  }
  
})
